package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;


	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;


    @Override
    public List<JSONObject> getBaseCategoryList() {
		//创建最大的集合，放一级分类的JSONObject
		ArrayList<JSONObject> list = new ArrayList<>();
		//通过视图查询所有分类数据：一级，二级，三级，数据格式见数据库的view
		//list集合中每一个view代表数据库中一条包含三级分类的数据
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);
		//将这个list集合转化为map，key为一级分类id，value为List<BaseCategoryView>，list包含相同一级分类id
		//的所有数据,以一级分类id分组
		Map<Long, List<BaseCategoryView>> map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		//遍历当前map集合，该map的key是一级分类的所有key,value是一，二，三级分类的所有数据
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = map.entrySet().iterator();
		//每次循环得到一个一级分类所有数据
		while (iterator.hasNext()){
			//获取一个一级分类，及其对应的数据
			Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
			//一级分类的的id
			Long category1Id = entry.getKey();
			//一个一级分类对应的所有数据
			List<BaseCategoryView> categoryViewList = entry.getValue();
			// 用于存储一个一级分类的JSON
			JSONObject category1 = new JSONObject();
			category1.put("categoryId",category1Id);
			//从集合中获取，里边有完整数据
			category1.put("categoryName",categoryViewList.get(0).getCategory1Name());
			//	声明一个集合来存储二级分类对象(从返回的json对象可以看出结构)
			ArrayList<JSONObject> category2ChildList = new ArrayList<>();
			//遍历当前一级分类对应的二级分类并分别放入一个JSONObject，最后放入上边声明的数组
			//key为二级分类的每一个id，value为相同二级分类的数据
			Map<Long, List<BaseCategoryView>> map1 = categoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = map1.entrySet().iterator();
			while (iterator1.hasNext()){
				Map.Entry<Long, List<BaseCategoryView>> entry1 = iterator1.next();
				Long category2Id = entry1.getKey();
				//一个二级分类对应的所有数据
				List<BaseCategoryView> viewList = entry1.getValue();
				//创建一个二级对象JSONObject
				JSONObject category2 = new JSONObject();
				category2.put("categoryId",category2Id);
				category2.put("categoryName",viewList.get(0).getCategory2Name());
				//获取相同二级分类对应的三级分类集合
				List<JSONObject> category3List = viewList.stream().map(baseCategoryView -> {
					//创建一个JSONObject保存三级对象
					JSONObject category3 = new JSONObject();
					Long category3Id = baseCategoryView.getCategory3Id();
					String category3Name = baseCategoryView.getCategory3Name();
					category3.put("categoryId", category3Id);
					category3.put("categoryName", category3Name);
					return category3;
				}).toList();
				//将三级分类集合放入二级分类的categoryChild中
				category2.put("categoryChild",category3List);
				//每遍历完一个一级分类的二级分类，将其加入到一级分类的categoryChild集合中
				category2ChildList.add(category2);
			}
			//将遍历完的二级分类集合加入到一级分类的的categoryChild
			category1.put("categoryChild",category2ChildList);
			//每遍历完一个一级分类，将其加入到最大的集合里
			list.add(category1);
		}
		return list;
	}

    @Override
    public List<BaseAttribute> findAttribute(Long category1Id) {
		return baseAttributeMapper.selectAttribute(category1Id);
    }

    @Override
    public BaseCategoryView getCategoryView(Long category3Id) {

		return baseCategoryViewMapper.selectById(category3Id);

	}

	public JSONObject getBaseCategoryById(Long category1Id) {
		//	创建对象
		JSONObject category1 = new JSONObject();
		//	查询一级分类表：
		BaseCategory1 baseCategory1 = baseCategory1Mapper.selectById(category1Id);
		LambdaQueryWrapper<BaseCategory2> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(BaseCategory2::getCategory1Id, baseCategory1.getId());
		category1.put("categoryId", category1Id);
		category1.put("categoryName", baseCategory1.getName());
		//	查询二级分类数据
		List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(wrapper);
		List<JSONObject> categoryChild2List = baseCategory2List.stream().map(baseCategory2 -> {
			//	创建二级对象
			JSONObject category2 = new JSONObject();
			category2.put("categoryId", baseCategory2.getId());
			category2.put("categoryName", baseCategory2.getName());
			//	获取三级分类对象
			List<BaseCategory3> baseCategory3List = this.baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>().eq(BaseCategory3::getCategory2Id, baseCategory2.getId()));
			List<JSONObject> categoryChild3List = baseCategory3List.stream().map(baseCategory3 -> {
				//	创建对象
				JSONObject category3 = new JSONObject();
				category3.put("categoryId", baseCategory3.getId());
				category3.put("categoryName", baseCategory3.getName());
				//	返回数据
				return category3;
			}).collect(Collectors.toList());
			category2.put("categoryChild", categoryChild3List);
			//	返回二级分类对象
			return category2;
		}).collect(Collectors.toList());
		//  存储数据
		category1.put("categoryChild", categoryChild2List);
		//	返回数据
		return category1;
	}
}
